package com.ncdnapp.config.db;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.noear.solon.annotation.*;


@Configuration
public class MysqlDataSourceConfig {
    @Inject("${mybatis-flex1.datasource.master.jdbcUrl}")
    private String jdbcUrl;
    @Inject("${mybatis-flex1.datasource.master.username}")
    private String userName;
    @Inject("${mybatis-flex1.datasource.master.password}")
    private String password;
    @Inject("${mybatis-flex1.datasource.master.driver-class-name}")
    private String driverClassName;
    @Inject("${mybatis-flex1.datasource.master.hikari.maximum-pool-size}")
    private int maxPoolSize;
    @Inject("${mybatis-flex1.datasource.master.hikari.minimum-idle}")
    private int minIdle;
    @Inject("${mybatis-flex1.datasource.master.hikari.max-lifetime}")
    private long maxLifetimeMs;

    @Bean(name = "lkDataSource")
    public BaseDataSource init() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName(driverClassName);
        config.setJdbcUrl(jdbcUrl);
        config.setUsername(userName);
        config.setPassword(password);
        config.setMinimumIdle(minIdle);
        config.setMaximumPoolSize(maxPoolSize);
        config.setMaxLifetime(maxLifetimeMs);
        config.setPoolName("lk");
        config.setRegisterMbeans(true);

        HikariDataSource dataSource = new HikariDataSource(config);
        return new BaseDataSource("lk", dataSource);
    }
}
